#include "toolkit.h"

#include "SC_Init.h"	//MCU��ʼ��ͷ�ļ��������̼�������ͷ�ļ�
#include "SC_it.h"
#include "sc95f.h"

#include "stdio.h"

extern bit Uart0SendFlag;
extern bit Uart0ReceiveFlag;

extern bit Uart1SendFlag;
extern bit Uart1ReceiveFlag;

extern uint32_t get_Systick(void);

#define SYS_MESSAGE_QUEUE_NUM_MAX 5
message_queue_t gMessageQueue[SYS_MESSAGE_QUEUE_NUM_MAX] = {0};
uint32_t gMessageQueueHead = 0;
uint32_t gMessageQueueTail = 0;



uint8_t device_timeout_count(uint32_t* time_enter,uint32_t time_out)
{
	uint32_t* tick_enter = (uint32_t*)time_enter;
	if (*tick_enter == 0){
		*tick_enter = get_Systick();
		return FIRST_ENTER;
	} else {

			if ((get_Systick() - (*tick_enter)) >= time_out){
				*tick_enter = 0;
				return TIME_OUT_WORK;
			} else {
				return TIME_NOT_OUT;
			}
	}
}

//char putchar(char c)
//{
//	US0CON3 = c;
//	while(!Uart0SendFlag);
//	Uart0SendFlag = 0;
//  return c;
//}

int message_queue_init()
{
	
	return 0;
}

//int message_queue_send(message_queue_t* msg)
//{
//	if(msg == NULL)
//		return -1;
//	if((msg->source == MESSAGE_SOURCE_NONE) || (msg->command == 0))
//		return -2;
//	
//	gMessageQueue[gMessageQueueTail].source = msg->source;
//	gMessageQueue[gMessageQueueTail].command = msg->command;
//	gMessageQueue[gMessageQueueTail].value = msg->value;
//	gMessageQueue[gMessageQueueTail].activeFlag = 1;
//	
//	gMessageQueueTail = gMessageQueueTail + 1;
//	gMessageQueueTail = gMessageQueueTail%SYS_MESSAGE_QUEUE_NUM_MAX;
//	
//	return 0;
//}

//int message_queue_recv(message_queue_t* msg, uint32_t waitMs)
//{
//	if(msg == NULL)
//		return -1;
//	if(gMessageQueueTail != gMessageQueueHead)//queue is not empty.
//	{
//		if(gMessageQueue[gMessageQueueHead].activeFlag)
//		{
//			msg->source = gMessageQueue[gMessageQueueHead].source;
//			msg->command = gMessageQueue[gMessageQueueHead].command;
//			msg->value = gMessageQueue[gMessageQueueHead].value;
//			gMessageQueue[gMessageQueueHead].activeFlag = 0;
//		}
//		gMessageQueueHead = gMessageQueueHead + 1;
//		gMessageQueueHead = gMessageQueueHead%SYS_MESSAGE_QUEUE_NUM_MAX;
//		
//		return 0;
//	}
//	
//	return -2;
//}

char putchar(char c)
{
	US1CON3 = c;
	while(!Uart1SendFlag);
	Uart1SendFlag = 0;
  return c;
}





 